// Copyright (C) 1997-2002 Alias|Wavefront,
// a division of Silicon Graphics Limited.
//
// The information in this file is provided for the exclusive use of the
// licensees of Alias|Wavefront.  Such users have the right to use, modify,
// and incorporate this code into other products for purposes authorized
// by the Alias|Wavefront license agreement, without fee.
//
// ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
// EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
// DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
//
//textureEditorIsolateSelect.mel
//
//must be in view faces of selected images mode to see effects
//
// $mode =
//			0: remove all visible for selected object
//			1: add selected to visible set
//			2: remove selected from visible set

global proc textureEditorIsolateSelect(int $mode){
	
	// determine if textureEditorIsolateSelectSet exists
	string $setExists[] = `ls textureEditorIsolateSelectSet`;
	if (size($setExists) == 0)
		sets -name "textureEditorIsolateSelectSet"
			-facets true;
	
	// Begin changing the set
	string $objects[] = `ls -sl`;
	for ($object in $objects){
		
		//convert selection to faces
		string $faces[] = `polyListComponentConversion 
			-ff -fv -fe -fuv -fvf -tf $object`;
		//if there are no faces then stop here and inform the user
		if (size($faces) == 0){
			error "Texture Editor Isolate Select only works with polygonal meshes.";
		}
		
		switch( $mode ){
		case 0:
			string $shape[] = `listRelatives -parent -shapes $faces[0]`;
			clear $faces;
			$faces = `polyListComponentConversion -fv -fe -fuv -fvf -tf $shape[0]`;
			sets -edit 
				-remove textureEditorIsolateSelectSet $faces;
			// this is the end for this case so return
			return;
			break;
			
		case 1:
			sets -edit 
				-addElement textureEditorIsolateSelectSet $faces;
			break;
			
		case 2:
			sets -edit 
				-remove textureEditorIsolateSelectSet $faces;
			break;
		}
		
		string $parent[] = `listRelatives -parent $object`;
		
		// Find out the groupId nodes connected to the mesh
		string $meshGroupIdNode[] = `listConnections -type groupId
			$parent[0]`;
		string $setGroupIdNode[] = `listConnections -type groupId
			textureEditorIsolateSelectSet`;
		
		// create an intersector to find the proper groupId
		string $groupIdIntersector = `stringArrayIntersector`;
		stringArrayIntersector -edit -intersect $meshGroupIdNode $groupIdIntersector;
		stringArrayIntersector -edit -intersect $setGroupIdNode $groupIdIntersector;
		string $groupIdNode[] = `stringArrayIntersector -query $groupIdIntersector`;
		
		// Get the magic number from that node
		int $groupId = `getAttr ($groupIdNode[0] +".groupId")`;

		// Set that magic number to the meshShape
		setAttr ($parent[0] + ".displayFacesWithGroupId") $groupId;
				
	}
}

